package rkw.database.repositories;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import rkw.database.model.Election;
import rkw.database.model.ElectionVoter;
import rkw.database.model.Voter;

@Transactional
public interface ElectionVoterRepository extends JpaRepository<ElectionVoter, Long> {

	ElectionVoter findByVoterAndElection(Voter voter, Election election);

	@Query("SELECT ev FROM ElectionVoter ev WHERE ev.voter.identificationField = :voterId and ev.election.identificationField = :electionId")
	ElectionVoter findByElectionIdAndVoterId(@Param("electionId") Long electionIdentificationField, @Param("voterId") String voterIdentificationField);

	@Query("SELECT ev FROM ElectionVoter ev INNER JOIN ev.voter v WHERE ev.election = :election AND (LOWER(v.name) LIKE :text OR LOWER(v.surname) LIKE :text OR LOWER(v.address) LIKE :text OR LOWER(v.identificationField) LIKE :text)")
	List<ElectionVoter> findAllLike(@Param("election") Election election, @Param("text") String text);
}
